<?php
require_once "header_before_menu.php";
require_once "db.php";
require_once "util.php";
require_once "header_editor.php";

get_database_connection();

$id = $_REQUEST["id"];

// Getting the data of this slide.
$sql = "SELECT * FROM slides WHERE id = $id";
$query = mysql_query($sql);

$slide_assoc = mysql_fetch_assoc($query);
$slide_object_id = $slide_assoc["object_id"];
$slide_current_position = $slide_assoc["position"];
$slide_desired_position = $slide_current_position + 1;

// Getting the max and lower position of this object.
$sql = "SELECT position FROM slides WHERE object_id = $slide_object_id ORDER BY position DESC LIMIT 1";
$query = mysql_query($sql);
$assoc = mysql_fetch_assoc($query);
$max_position = $assoc["position"];


$sql = "SELECT position FROM slides WHERE object_id = $slide_object_id ORDER BY position ASC LIMIT 1";
$query = mysql_query($sql);
$assoc = mysql_fetch_assoc($query);
$min_position = $assoc["position"];


if (($slide_desired_position <= $max_position) && ($slide_desired_position >= $min_position)) {
  $sql = "UPDATE slides SET position = null WHERE id = $id";
  $res = mysql_query($sql);

  $sql = "UPDATE slides SET position = $slide_current_position 
          WHERE position = $slide_desired_position AND object_id = $slide_object_id";
  $res = mysql_query($sql);

  $sql = "UPDATE slides SET position = $slide_desired_position WHERE id = $id";
  $res = mysql_query($sql);
}  

set_flash("Movimentacao da pagina concluida.");
ddd_redirect("index.editor.php");
?>



